﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYElectricSetting;

namespace HYCableTrayConnection
{
	internal class CableTrayFitingSel
	{
		public static FamilySymbol GetCableTrayFittingType(Document doc, CableTrayFittingType cableTrayFittingType, HYCableTray<CableTray> cableTray)
		{
			CableTrayType ctt = doc.GetElement(cableTray.CableTray.GetTypeId()) as CableTrayType;
			FamilySymbol familySymbol = CableTrayFitingSel.GetCableTrayDefaultFittingType(doc, cableTrayFittingType, ctt);
			if (familySymbol != null)
			{
				return familySymbol;
			}
			familySymbol = CableTrayFitingSel.GetDocumentFittingType(doc, cableTrayFittingType, ctt);
			if (familySymbol != null)
			{
				return familySymbol;
			}
			return CableTrayFitingSel.GetInstallFittingType(doc, cableTrayFittingType, ctt);
		}

		public static FamilySymbol GetVerticalCableTrayFittingType(Document doc, VerticalCableTrayFittingType verticalCableTrayFittingType, HYCableTray<CableTray> cableTray)
		{
			CableTrayType cableTrayType = doc.GetElement(cableTray.CableTray.GetTypeId()) as CableTrayType;
			string path = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), "CableTrayFitting");
			string str = "";
			if ((int)cableTrayType.ShapeType == 1)
			{
				str = "槽式";
			}
			else if ((int)cableTrayType.ShapeType == 2)
			{
				str = "梯式";
			}
			VerticalCableTrayFittingInfo verticalCableTrayFittingInfo = (from f in CableTrayFitingSel.GetVerticalCableTrayFittingInfo()
			where f.Key == verticalCableTrayFittingType
			select f).Single<VerticalCableTrayFittingInfo>();
			string text = str + verticalCableTrayFittingInfo.Name;
			Family family = null;
			FamilySymbol result = null;
			foreach (Element element in RevitFilter.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_CableTrayFitting).ToElements())
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				if (familySymbol.Family.Name == text)
				{
					return familySymbol;
				}
			}
			string text2 = Path.Combine(path, text + ".rfa");
			doc.LoadFamily(text2, out family);
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						result = enumerator2.Current;
					}
				}
			}
			return result;
		}

		private static List<VerticalCableTrayFittingInfo> GetVerticalCableTrayFittingInfo()
		{
			return new List<VerticalCableTrayFittingInfo>
			{
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.LeftDownSideElbow,
					Name = "垂直左下弯通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.RightDownSideElbow,
					Name = "垂直右下弯通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.LeftUpSideElbow,
					Name = "垂直左上弯通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.RightUpSideElbow,
					Name = "垂直右上弯通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpTee,
					Name = "上垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownTee,
					Name = "下垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpSideTee,
					Name = "上边垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownSideTee,
					Name = "下边垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownCornerTeeA,
					Name = "下角垂直三通-A"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownCornerTeeB,
					Name = "下角垂直三通-B"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpCornerTeeA,
					Name = "上角垂直三通-A"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpCornerTeeB,
					Name = "上角垂直三通-B"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalLeftTee,
					Name = "左垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalRightTee,
					Name = "右垂直三通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalLeftCross,
					Name = "左垂直四通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalRightCross,
					Name = "右垂直四通"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpCrossA,
					Name = "上垂直四通-A"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpCrossB,
					Name = "上垂直四通-B"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalUpCrossC,
					Name = "上垂直四通-C"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownCrossA,
					Name = "下垂直四通-A"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownCrossB,
					Name = "下垂直四通-B"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalDownCrossC,
					Name = "下垂直四通-C"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalCrossA,
					Name = "垂直四通-A"
				},
				new VerticalCableTrayFittingInfo
				{
					Key = VerticalCableTrayFittingType.VerticalCrossB,
					Name = "垂直四通-B"
				}
			};
		}

		private static FamilySymbol GetCableTrayDefaultFittingType(Document doc, CableTrayFittingType cableTrayFittingType, CableTrayType ctt)
		{
			ElementId elementId = null;
			FamilySymbol result = null;
			switch (cableTrayFittingType)
			{
			case CableTrayFittingType.HorizontalElbow:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM).AsElementId();
				break;
			case CableTrayFittingType.VerticalInnerElbow:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM).AsElementId();
				break;
			case CableTrayFittingType.VerticalOuterElbow:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM).AsElementId();
				break;
			case CableTrayFittingType.HorizontalTee:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM).AsElementId();
				break;
			case CableTrayFittingType.HorizontalCross:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM).AsElementId();
				break;
			case CableTrayFittingType.Transition:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM).AsElementId();
				break;
			case CableTrayFittingType.Union:
				elementId = ctt.GetParameter(BuiltInParameter.RBS_CURVETYPE_DEFAULT_UNION_PARAM).AsElementId();
				break;
			}
			if (elementId != null)
			{
				result = (doc.GetElementById(elementId) as FamilySymbol);
			}
			return result;
		}

		private static FamilySymbol GetDocumentFittingType(Document doc, CableTrayFittingType cableTrayFittingType, CableTrayType ctt)
		{
			FamilySymbol familySymbol = null;
			BuiltInParameter parameterId = BuiltInParameter.REBAR_ELEM_HOST_MARK;
			int num = -1;
			if ((int)ctt.ShapeType == 1)
			{
				switch (cableTrayFittingType)
				{
				case CableTrayFittingType.HorizontalElbow:
					num = 35;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM;
					break;
				case CableTrayFittingType.VerticalInnerElbow:
					num = 36;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM;
					break;
				case CableTrayFittingType.VerticalOuterElbow:
					num = 36;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM;
					break;
				case CableTrayFittingType.HorizontalTee:
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM;
					num = 38;
					break;
				case CableTrayFittingType.HorizontalCross:
					num = 37;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM;
					break;
				case CableTrayFittingType.Transition:
					num = 39;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM;
					break;
				case CableTrayFittingType.Union:
					num = 40;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_UNION_PARAM;
					break;
				}
			}
			else if ((int)ctt.ShapeType == 2)
			{
				switch (cableTrayFittingType)
				{
				case CableTrayFittingType.HorizontalElbow:
					num = 43;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM;
					break;
				case CableTrayFittingType.VerticalInnerElbow:
					num = 44;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM;
					break;
				case CableTrayFittingType.VerticalOuterElbow:
					num = 44;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM;
					break;
				case CableTrayFittingType.HorizontalTee:
					num = 46;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM;
					break;
				case CableTrayFittingType.HorizontalCross:
					num = 45;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM;
					break;
				case CableTrayFittingType.Transition:
					num = 47;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM;
					break;
				case CableTrayFittingType.Union:
					num = 48;
					parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_UNION_PARAM;
					break;
				}
			}
			if (num == -1)
			{
				return familySymbol;
			}
			foreach (Element element in RevitFilter.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_CableTrayFitting).ToElements())
			{
				familySymbol = (element as FamilySymbol);
				if (familySymbol.Family.GetParameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == num)
				{
					ctt.GetParameter(parameterId).Set(familySymbol.Id);
					break;
				}
			}
			return familySymbol;
		}

		private static FamilySymbol GetInstallFittingType(Document doc, CableTrayFittingType cableTrayFittingType, CableTrayType ctt)
		{
			FamilySymbol familySymbol = null;
			BuiltInParameter parameterId = BuiltInParameter.REBAR_ELEM_HOST_MARK;
			int category = 0;
			int subCategory = 0;
			if ((int)ctt.ShapeType == 1)
			{
				category = 1;
			}
			else if ((int)ctt.ShapeType == 2)
			{
				category = 2;
			}
			switch (cableTrayFittingType)
			{
			case CableTrayFittingType.HorizontalElbow:
				subCategory = 1;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_HORIZONTAL_BEND_PARAM;
				break;
			case CableTrayFittingType.VerticalInnerElbow:
				subCategory = 2;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWUP_PARAM;
				break;
			case CableTrayFittingType.VerticalOuterElbow:
				subCategory = 3;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_ELBOWDOWN_PARAM;
				break;
			case CableTrayFittingType.HorizontalTee:
				subCategory = 4;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TEE_PARAM;
				break;
			case CableTrayFittingType.HorizontalCross:
				subCategory = 5;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_CROSS_PARAM;
				break;
			case CableTrayFittingType.Transition:
				subCategory = 6;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_TRANSITION_PARAM;
				break;
			case CableTrayFittingType.Union:
				subCategory = 7;
				parameterId = BuiltInParameter.RBS_CURVETYPE_DEFAULT_UNION_PARAM;
				break;
			}
			string family = CableTrayFitingSel.GetFamilyDatas("桥架配件", category, subCategory)[0].Family;
			Family family2;
			doc.LoadFamily(family, new MyFamilyLoadOptions(), out family2);
			if (family2 != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator = family2.GetSymbols().GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						familySymbol = enumerator.Current;
					}
				}
				ctt.GetParameter(parameterId).Set(familySymbol.Id);
			}
			return familySymbol;
		}

		private static List<FittingSelectData> GetFamilyDatas(string tableName, int category, int subCategory)
		{
			List<FittingSelectData> list = new List<FittingSelectData>();
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			foreach (ListItem listItem in spaceFamilyDataReader.GetAllName(tableName))
			{
				int id = Convert.ToInt32(listItem.Tag);
				FittingSelectData fittingSelectData = new FittingSelectData();
				foreach (ListItem item in spaceFamilyDataReader.GetInfoById(tableName, id))
				{
					CableTrayFittingDataInfo.SetValueForFamilyObject(fittingSelectData, item);
				}
				if (fittingSelectData.Category != 0 && fittingSelectData.Category == category && fittingSelectData.SubCategory == subCategory)
				{
					list.Add(fittingSelectData);
				}
			}
			return list;
		}
	}
}
